package com.lanou3g.zjl.chating.home.friends;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou3g.zjl.chating.R;
import com.lanou3g.zjl.chating.base.BaseActivity;
import com.lanou3g.zjl.chating.home.personal_center.CircleImageView;
import com.lanou3g.zjl.chating.utils.GetUserAvatarHelper;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemLongClick;

/**
 * Created by dllo on 15/11/27.
 */
public class GroupBlackListActivity extends BaseActivity {
    private String groupId;
    private List<String> groupBlackList;
    private Handler handler;
    private BlackListAdapter adapter;
    @Bind(R.id.groupBlackListView)
    ListView groupBlackListView;

    @Override
    protected int getLayout() {
        return R.layout.group_black_list;
    }

    @Override
    protected void initData() {
        groupId = getIntent().getStringExtra("groupID");
        getAsynk();
        getBlackList();
    }

    @OnItemLongClick(R.id.groupBlackListView)
    boolean moveUser(final int position, View view) {
        PopupMenu popupMenu = new PopupMenu(this, view);
        popupMenu.getMenuInflater().inflate(R.menu.moveout_black_listuser, popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.moveOut:
                        moveOutBlackMembers(adapter.getItem(position));
                        break;
                }
                return true;
            }
        });
        popupMenu.show();
        return false;
    }

    @OnClick(R.id.finishGroupBalckList)
    void finishGroupBalckList() {
        finish();
    }

    //接收子线程消息
    private void getAsynk() {
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                switch (message.what) {
                    case 1:
                        adapter = new BlackListAdapter();
                        groupBlackListView.setAdapter(adapter);
                        break;
                    case 2:
                        //刷新
                        getBlackList();
                        Toast.makeText(GroupBlackListActivity.this, "移除成功", Toast.LENGTH_SHORT).show();
                        break;
                }
                return false;
            }
        });
    }

    //获取群黑名单列表
    private void getBlackList() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    groupBlackList = EMGroupManager.getInstance().getBlockedUsers(groupId);
                    handler.sendEmptyMessage(1);
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //将黑名单中成员移除
    private void moveOutBlackMembers(final String username) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    EMGroupManager.getInstance().unblockUser(groupId, username);
                    handler.sendEmptyMessage(2);
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public class BlackListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return groupBlackList != null && groupBlackList.size() > 0 ? groupBlackList.size() : 0;
        }

        @Override
        public String getItem(int i) {
            return groupBlackList != null && groupBlackList.size() > 0 ? groupBlackList.get(i) : null;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder = null;
            String name = getItem(i);
            if (view == null) {
                view = LayoutInflater.from(GroupBlackListActivity.this).inflate(R.layout.group_blacklist_item, null);
                holder = new ViewHolder(view);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            holder.groupBlackListName.setText(name);
            GetUserAvatarHelper helper = new GetUserAvatarHelper(GroupBlackListActivity.this, holder.groupBlackPic, name);
            helper.execute(name);
            return view;
        }

        class ViewHolder {
            @Bind(R.id.groupBlackPic)
            CircleImageView groupBlackPic;
            @Bind(R.id.groupBlackListName)
            TextView groupBlackListName;

            ViewHolder(View view) {

                ButterKnife.bind(this, view);
            }
        }
    }
}
